package com.jack.payment.aspect;

import com.jack.payment.annotation.PagingLimit;
import com.jack.payment.entity.common.CommonAskParam;
import com.jack.payment.util.common.StringUtil;
import com.jack.payment.util.common.TimeUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Order(2)
@Component
public class PagingLimitAspect {
    private Logger log = LoggerFactory.getLogger(PagingLimitAspect.class);

    @Pointcut("@annotation(com.jack.payment.annotation.PagingLimit)")
    public void resultService(){
    }

    @Around("resultService()")
    public <T>T doAround(ProceedingJoinPoint pjp) throws Throwable {
        Object[] args = pjp.getArgs();
        Method method = ((MethodSignature) pjp.getSignature()).getMethod();
        CommonAskParam commonAskParam = (CommonAskParam) args[0];

        Object originCollection = commonAskParam.getData();
        Class originClass = originCollection.getClass();
        Method getOriginCount = originClass.getMethod("getCount");
        Method getOriginPage = originClass.getMethod("getPage");

        Integer count = (Integer) getOriginCount.invoke(originCollection);
        Integer page = (Integer) getOriginPage.invoke(originCollection);

        if (count == null || count <= 2 || count >= 40) {
            count = method.getAnnotation(PagingLimit.class).count();
        }
        if (page == null || page < 0)  {
            page = 0;
        }

        Method getOriginStartTime = originClass.getMethod("getFilterStartTime");
        Method getOriginEndTime = originClass.getMethod("getFilterEndTime");
        String startTime = (String)getOriginStartTime.invoke(originCollection);
        String endTime = (String)getOriginEndTime.invoke(originCollection);
        if (!StringUtil.isEmpty(startTime)) {
            startTime = TimeUtil.timeConversion(startTime).toString();
            originClass.getMethod("setFilterStartTime", String.class).invoke(originCollection, startTime);
        }
        if (!StringUtil.isEmpty(endTime)) {
            endTime = TimeUtil.timeConversion(endTime).toString();
            if (!StringUtil.isEmpty(startTime)) {
                if (Long.parseLong(endTime) <= Long.parseLong(startTime)) {
                    originClass.getMethod("setFilterStartTime", String.class).invoke(originCollection, null);
                }
            }
            originClass.getMethod("setFilterEndTime", String.class).invoke(originCollection, endTime);
        }

        Method setPage = originClass.getMethod("setPage", Integer.class);
        Method setCount = originClass.getMethod("setCount", Integer.class);
        setCount.invoke(originCollection, count);
        setPage.invoke(originCollection, page * count);
        commonAskParam.setData(originCollection);
        args[0] = commonAskParam;
        return (T) pjp.proceed(args);
    }
}
